﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class TextBox : ICommandUIDefinition, IControlsChild, IXmlBuildable
    {
        public string Command { get; set; }
        public string Id { get; set; }
        public bool? ImeEnabled { get; set; }
        public int? MaxLength { get; set; }
        public string QueryCommand { get; set; }
        public int? Sequence { get; set; }
        public string ShowAsLabel { get; set; }
        public string TemplateAlias { get; set; }
        public string ToolTipDescription { get; set; }
        public string ToolTipHelpKeyWord { get; set; }
        public string ToolTipImage32by32 { get; set; }
        public string ToolTipImage32by32Class { get; set; }
        public int? ToolTipImage32by32Left { get; set; }
        public int? ToolTipImage32by32Top { get; set; }
        public string ToolTipShortcutKey { get; set; }
        public string ToolTipTitle { get; set; }
        public string Width { get; set; }

        public TextBox()
        { }

        public TextBox(XmlNode node)
        {
            try
            {
                this.Command = XmlHelper.GetAttribute(node, "Command");
                this.Id = XmlHelper.GetAttribute(node, "Id");
                this.ImeEnabled = XmlHelper.GetAttribute(node, "ImeEnabled") == "true";
                this.MaxLength = XmlHelper.GetNullableIntAttribute(node, "MaxLength");
                this.QueryCommand = XmlHelper.GetAttribute(node, "QueryCommand");
                this.Sequence = (int?)XmlHelper.ExtractXmlAttributeToProperty2(node, "Sequence", XmlHelper.PropertyType.integerType);
                this.ShowAsLabel = XmlHelper.GetAttribute(node, "ShowAsLabel");
                this.TemplateAlias = XmlHelper.GetAttribute(node, "TemplateAlias");
                this.ToolTipDescription = XmlHelper.GetAttribute(node, "ToolTipDescription");
                this.ToolTipHelpKeyWord = XmlHelper.GetAttribute(node, "ToolTipHelpKeyWord");
                this.ToolTipImage32by32 = XmlHelper.GetAttribute(node, "ToolTipImage32by32");
                this.ToolTipImage32by32Class = XmlHelper.GetAttribute(node, "ToolTipImage32by32Class");
                this.ToolTipImage32by32Left = XmlHelper.GetNullableIntAttribute(node, "ToolTipImage32by32Left");
                this.ToolTipImage32by32Top = XmlHelper.GetNullableIntAttribute(node, "ToolTipImage32by32Top");
                this.ToolTipShortcutKey = XmlHelper.GetAttribute(node, "ToolTipShortcutKey");
                this.ToolTipTitle = XmlHelper.GetAttribute(node, "ToolTipTitle");
                this.Width = XmlHelper.GetAttribute(node, "Width");
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode commandUINode)
        {
            try
            {
                XmlNode textBoxNode = XmlHelper.AddNode(commandUINode, "TextBox");
                XmlHelper.AddAttribute(textBoxNode, "Command", this.Command);
                XmlHelper.AddAttribute(textBoxNode, "Id", this.Id);
                XmlHelper.AddAttribute(textBoxNode, "ImeEnabled", this.ImeEnabled);
                XmlHelper.AddAttribute(textBoxNode, "MaxLength", this.MaxLength);
                XmlHelper.AddAttribute(textBoxNode, "QueryCommand", this.QueryCommand);
                XmlHelper.AddAttribute(textBoxNode, "Sequence", this.Sequence);
                XmlHelper.AddAttribute(textBoxNode, "ShowAsLabel", this.ShowAsLabel);
                XmlHelper.AddAttribute(textBoxNode, "TemplateAlias", this.TemplateAlias);
                XmlHelper.AddAttribute(textBoxNode, "ToolTipDescription", this.ToolTipDescription);
                XmlHelper.AddAttribute(textBoxNode, "ToolTipHelpKeyWord", this.ToolTipHelpKeyWord);
                XmlHelper.AddAttribute(textBoxNode, "ToolTipImage32by32", this.ToolTipImage32by32);
                XmlHelper.AddAttribute(textBoxNode, "ToolTipImage32by32Class", this.ToolTipImage32by32Class);
                XmlHelper.AddAttribute(textBoxNode, "ToolTipImage32by32Left", this.ToolTipImage32by32Left);
                XmlHelper.AddAttribute(textBoxNode, "ToolTipImage32by32Top", this.ToolTipImage32by32Top);
                XmlHelper.AddAttribute(textBoxNode, "ToolTipShortcutKey", this.ToolTipShortcutKey);
                XmlHelper.AddAttribute(textBoxNode, "ToolTipTitle", this.ToolTipTitle);
                XmlHelper.AddAttribute(textBoxNode, "Width", this.Width);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("TextBox");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, null, true);

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
